package b2.widget.login
{
	import b2.config.*;
	import b2.net.*;
	import b2.player.PlayerInfo;
	import b2.player.PlayerInfoManager;
	import b2.util.*;
	import b2.widget.*;
	import b2.widget.tab.*;
	
	import com.greensock.TweenLite;
	
	import flash.events.*;
	import flash.filters.*;
	import flash.text.*;
	import flash.external.*;
	
	public class LoginDialog extends TabController
	{
		private static var instance:LoginDialog;
		private var loginDays:int;
		private var fetchGiftButton:BasicButton;
		
		public function LoginDialog(loginDays:int)
		{
			this.loginDays = Math.min(loginDays, 8);
			var LoginDialogBgr:Class = ResourceHelper.getInstance().getClassByName("LoginDialogBgr");
			var LoginTitleBgr:Class = ResourceHelper.getInstance().getClassByName("LoginTitleBgr");
			var LoginTitleBgr2:Class = ResourceHelper.getInstance().getClassByName("LoginTitleBgr2");
			var LoginTitleBgr3:Class = ResourceHelper.getInstance().getClassByName("LoginTitleBgr3");
			var CloseButton:Class = ResourceHelper.getInstance().getClassByName("CloseButton");
			super(LoginDialogBgr, CloseButton);
			//标题
			var titleText:TextField = new TextField();
			titleText.selectable = false;
        	titleText.autoSize = TextFieldAutoSize.CENTER;
        	var titleFomat:TextFormat = new TextFormat("微软雅黑", 16, 0xFFFFFF);
        	titleFomat.letterSpacing = 10;
			titleText.defaultTextFormat = titleFomat;
			titleText.filters = [new GlowFilter(0x000000, 1, 1.3, 1.3, 100, BitmapFilterQuality.HIGH)];
			titleText.text = "军部签到";
			titleText.x = (this.width - titleText.width) / 2;
			titleText.y = 15;
			addChild(titleText);
			//连续登陆天数
			var loginDaysText:TextField = new TextField();
			loginDaysText.selectable = false;
        	loginDaysText.autoSize = TextFieldAutoSize.LEFT;
			loginDaysText.defaultTextFormat = new TextFormat("微软雅黑", 15, 0xFFFF00);
			loginDaysText.filters = [new GlowFilter(0x000000, 1, 1.3, 1.3, 100, BitmapFilterQuality.HIGH)];
			loginDaysText.text = "连续登陆天数";
			loginDaysText.x = 30;
			loginDaysText.y = 60;
			addChild(loginDaysText);
			//累积签到奖励
			var loginGiftText:TextField = new TextField();
			loginGiftText.selectable = false;
        	loginGiftText.autoSize = TextFieldAutoSize.LEFT;
			loginGiftText.defaultTextFormat = new TextFormat("微软雅黑", 15, 0xFFFF00);
			loginGiftText.filters = [new GlowFilter(0x000000, 1, 1.3, 1.3, 100, BitmapFilterQuality.HIGH)];
			loginGiftText.text = "累积签到奖励";
			loginGiftText.x = 45;
			loginGiftText.y = 150;
			addChild(loginGiftText);
			//领取奖励
			var ButtonBgr1:Class = ResourceHelper.getInstance().getClassByName("ButtonBgr1");
			var ButtonBgr2:Class = ResourceHelper.getInstance().getClassByName("ButtonBgr1");
			fetchGiftButton = new BasicButton(ButtonBgr1, "领取奖励", 12);
			fetchGiftButton.x = 120;
			fetchGiftButton.y = 280;
			fetchGiftButton.addEventListener(MouseEvent.CLICK, onFetchGiftButtonClick);
			var me:PlayerInfo = PlayerInfoManager.getInstance().players[PlayerInfoManager.getInstance().myUserId];
			if (me.hasFetchedGift())
			{
				fetchGiftButton.disable = true;
			}
			addChild(fetchGiftButton);
			//成为粉丝
			var fanButton:BasicButton = new BasicButton(ButtonBgr2, "邀请好友", 12);
			fanButton.x = 270;
			fanButton.y = 280;
			fanButton.addEventListener(MouseEvent.CLICK, onInviteButtonClick);
			addChild(fanButton);
			
			for (var i:int = 0; i < 8; i++)
			{
				var loginTab:LoginTab = new LoginTab(i);
				loginTab.hidePageSelector();
				if (i < loginDays)
				{
					addTab(LoginTitleBgr, loginTab.tabName, loginTab, LoginTitleBgr2);
				}
				else
					addTab(LoginTitleBgr3, loginTab.tabName, loginTab, LoginTitleBgr2);
			}
			this.setTabTitlePos(40, 89.6, 5);
			this.setTabPagePos(35, 185); 
			this.setCloseButtonPos(this.width - 44, 16);
			this.selectTab(this.loginDays - 1);
		}
		
		public function close():void
		{
			if (this.parent != null)
				this.parent.removeChild(this);
		}
		
		private function onFetchGiftButtonClick(event:MouseEvent):void
		{
			if (!fetchGiftButton.disable)
			{
				var objectBalance:String = "";
				selectTab(loginDays - 1);
				var loginTab:LoginTab = this.tabs[loginDays - 1] as LoginTab;
				for (var i:int = 0; i < loginTab.items.length; i++)
				{
					var giftItem:GiftItem = loginTab.items[i] as GiftItem;
					giftItem.determine();
					if (i > 0)
					{
						objectBalance += ",";
					}
					objectBalance += PlayerInfoManager.getInstance().myUserId + "," + giftItem.objectIndex + "," + giftItem.objBasicLevel + "," + giftItem.objStrengthenLevel + ",0," + giftItem.objNum;
				}
				TweenLite.delayedCall(1, startFetchGift, [objectBalance]);
				fetchGiftButton.disable = true;
			}
		}
		
		private function startFetchGift(objectBalance:String):void
		{
			ServerHelper.getInstance().fetchGift(objectBalance);
			ServerHelper.getInstance().addEventListener(ServerEvent.FETCH_GIFT, onFetchFinish);
		}
		
		private function onFetchFinish(event:ServerEvent):void
		{
			ServerHelper.getInstance().removeEventListener(ServerEvent.FETCH_GIFT, onFetchFinish);
			var loginTab:LoginTab = this.tabs[loginDays - 1] as LoginTab;
			var awardMessage:AwardMessage = new AwardMessage("每日签到领取成功，您获得以下奖励：", "明天继续登录将有更多奖励哦！", loginTab.items);
			WidgetManager.getInstance().popOut([awardMessage], true, true);
			this.close();
		}
		
		private function onInviteButtonClick(event:MouseEvent):void
		{
			ExternalInterface.call("tfInviteFriend");
		}
	}
}